package com.mvu.banana.domain.server;

import com.mvu.banana.domain.impl.KeyableBean;

import javax.persistence.EntityManager;

/**
 */
public class EM {

  public static long count(final Class<? extends KeyableBean> clazz) {
    return new BlockReturn<Long>() {
      @Override
      public Long run(EntityManager em) {
        return (Long) em.createQuery("select count(o) from " + clazz.getSimpleName() + " o")
                .getSingleResult();
      }
    }.run();
  }

  public static <T extends KeyableBean> T find(final Class<T> clazz, final Long id) {
    return new BlockReturn<T>() {
      @Override
      public T run(EntityManager em) {
        return em.find(clazz, id);
      }
    }.run();
  }
}
